package com.xy.xylive.hstapi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.xy.xylive.common.ServiceException;
import com.xy.xylive.config.HstConf;
import com.xy.xylive.hstapi.req.ChangeLayoutReq;
import com.xy.xylive.hstapi.req.LiveReq;
import com.xy.xylive.hstapi.req.PushReq;
import com.xy.xylive.hstapi.res.LayoutRes;
import com.xy.xylive.hstapi.res.PushRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class HstApiUtil {
    @Autowired
    private SignUtil signUtil;

    @Autowired
    private HstConf hstConf;

    /**
     * 创建直播
     * @param liveReq
     * @return
     */
    public HstApiResult<Integer> createLive(LiveReq liveReq) {
        log.info("创建直播请求参数:{}", JSONUtil.toJsonStr(liveReq));
        HttpResponse httpResponse = HttpUtil.createPost(hstConf.getHost() + "/paas/live/v1/room").addHeaders(signUtil.getAuthorizationHeader())
                .form(BeanUtil.beanToMap(liveReq)).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<Integer>>() {}, true);
    }

    /**
     * 删除直播
     * @param liveId
     * @return
     */
    public HstApiResult<Integer> deleteLive(String liveId) {
        log.info("删除直播请求参数: liveId {}", liveId);
        HttpResponse httpResponse = HttpUtil.createRequest(Method.DELETE, hstConf.getHost() + "/paas/live/v1/room/" + liveId).addHeaders(signUtil.getAuthorizationHeader()).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<Integer>>() {}, true);
    }

    /**
     * 直播开始结束控制
     * @param status start stop
     * @param liveId
     * @return
     */
    public HstApiResult controlLive(String status, String liveId) {
        log.info("直播开始结束控制请求参数:status {}, liveId {}", status, liveId);
        HttpResponse httpResponse = HttpUtil.createRequest(Method.POST, hstConf.getHost() + "/paas/live/v1/room/"+  liveId + "/" + status).addHeaders(signUtil.getAuthorizationHeader()).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult>() {}, true);
    }

    /**
     * 创建推流
     * @param liveId
     * @param pushReq
     * @return
     */
    public HstApiResult<PushRes> createPush(String liveId, PushReq pushReq) {
        log.info("创建推流请求参数:{}", JSONUtil.toJsonStr(pushReq));
        HttpResponse httpResponse = HttpUtil.createRequest(Method.POST, hstConf.getHost() + "/paas/live/v1/room/"+liveId+"/thirdparty-platform").addHeaders(signUtil.getAuthorizationHeader()).form(BeanUtil.beanToMap(pushReq)).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<PushRes>>() {}, true);
    }

    public HstApiResult<PushRes> updatePush(String liveId, PushReq pushReq) {
        log.info("更新推流请求参数:{}", JSONUtil.toJsonStr(pushReq));
        HttpResponse httpResponse = HttpUtil.createRequest(Method.PUT, hstConf.getHost() + "/paas/live/v1/room/"+liveId+"/thirdparty-platform").addHeaders(signUtil.getAuthorizationHeader()).form(BeanUtil.beanToMap(pushReq)).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<PushRes>>() {}, true);
    }

    public HstApiResult deletePush(String liveId, String platformId) {
        log.info("删除推流请求参数:liveId {} platformId {}", liveId, platformId);
        HttpResponse httpResponse = HttpUtil.createRequest(Method.DELETE, hstConf.getHost() + "/paas/live/v1/room/"+liveId+"/thirdparty-platform/" + platformId).addHeaders(signUtil.getAuthorizationHeader()).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), HstApiResult.class);
    }

//    public HstApiResult<List<PushRes>> getPush(String liveId, PushReq pushReq) {
//        HttpResponse httpResponse = HttpUtil.createRequest(Method.POST, hstConf.getHost() + "/paas/live/v1/room/"+liveId+"/thirdparty-platform").addHeaders(signUtil.getAuthorizationHeader()).form(BeanUtil.beanToMap(pushReq)).execute();
//        if(!httpResponse.isOk()) {
//            throw new ServiceException("好视通接口请求异常");
//        }
//        HstApiResult<PushRes> hstApiResult = JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<PushRes>>() {}, true);
//        if(!hstApiResult.isSuccess()) {
//            throw new ServiceException("好视通接口请求失败：" + hstApiResult.getResMessage());
//        }
//        return hstApiResult;
//    }

    public HstApiResult controlPush(String liveId, String platformId, String status) {
        log.info("推流控制请求参数:liveId {} platformId {} status {}", liveId, platformId, status);
        HttpResponse httpResponse = HttpUtil.createRequest(Method.POST, hstConf.getHost() + "/paas/live/v1/room/"+liveId+"/thirdparty-platform/" + platformId + "/" + status).addHeaders(signUtil.getAuthorizationHeader()).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), HstApiResult.class);
    }

    /**
     * 获取直播间布局
     * @return
     */
    public HstApiResult<LayoutRes> getLayout(String liveId) {
        HttpResponse httpResponse = HttpUtil.createRequest(Method.GET, hstConf.getHost() + "/paas/live/v2/room/"+liveId+"/layout-setting").addHeaders(signUtil.getAuthorizationHeader()).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), new TypeReference<HstApiResult<LayoutRes>>() {}, true);
    }

    /**
     * 切换直播间布局
     * 演讲者 画廊 切换
     * @return
     */
    public HstApiResult changeLayout(String liveId, ChangeLayoutReq changeLayoutReq) {
        log.info("切换直播间布局请求参数: liveId: {} , changeLayoutReq: {}", liveId, JSONUtil.toJsonStr(changeLayoutReq));
        HttpResponse httpResponse = HttpUtil.createRequest(Method.PUT, hstConf.getHost() + "/paas/live/v2/room/"+liveId+"/layout").addHeaders(signUtil.getAuthorizationHeader()).body(JSONUtil.toJsonStr(changeLayoutReq)).execute();
        if(!httpResponse.isOk()) {
            throw new ServiceException("好视通接口请求异常");
        }
        return JSONUtil.toBean(httpResponse.body(), HstApiResult.class);
    }




}
